import {
  Component,
  ChangeDetectionStrategy,
  OnInit
} from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd';
@Component({
  selector: 'system-company',
  templateUrl: './company.component.html',
  styleUrls: ['./company.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SystemCompanyComponent implements OnInit {
  loading = false;
  company: any;
  object: any;
  constructor(
    private http: _HttpClient,
    private msg: NzMessageService
  ) {}
  ngOnInit() {
    this.loading = true;
    this.http.get('api/company/list').subscribe((res: any) => {
      this.object = res;
      this.loading = false;
      if(this.object) {
        this.company = {
          title: this.object.title,
          logo: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
          website: this.object.website.website,
          startDate: this.object.startDate,
          contact: this.object.contact,
          weixin: this.object.weixin,
          qq: this.object.qq,
          address: this.object.address,
          describe: this.object.describe
        }
      }else{
        this.company = {
          title: '',
          logo: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
          website: '',
          startDate: '',
          contact: '',
          weixin: '',
          qq: '',
          address: '',
          describe: ''
        }
      }
    });
  }
  save() {
    let obj = this.company;
    this.loading = true;
    if(this.object&&this.object._id){
      obj['_id'] = this.object._id;
    }
    this.http.patch('api/company/update', obj).subscribe((res: any) => {
      this.loading = false;
      if(res.success) {
        this.msg.success(`修改成功`);
      }else{
        this.msg.error(`修改失败`);
      }
    }, () => {
      this.loading = false;
      this.msg.error(`网络请求失败`);
    });
  }
}
